package cn.com.fuhuitong.main.order.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.base.SafeMutableLiveData
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.entity.OrderAction
import cn.com.fuhuitong.http.BaseResponse
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.http.ViewModelSubscribeListener
import cn.com.fuhuitong.main.home.entity.MemberResponse
import cn.com.fuhuitong.main.home.mode.HomeManager
import cn.com.fuhuitong.main.host.entity.ConfigResponse
import cn.com.fuhuitong.main.mine.entity.AddressDefaultResponse
import cn.com.fuhuitong.main.order.entity.*
import cn.com.fuhuitong.main.order.mode.OrderManager
import com.blankj.utilcode.util.LogUtils
import io.reactivex.disposables.Disposable
import org.greenrobot.eventbus.EventBus

class OrderViewModel(application: Application) : BaseViewModel(application) {

    private val orderManager: OrderManager = OrderManager()

    val addressDefaultDataLiveData = MutableLiveData<HttpResponse<AddressDefaultResponse>>()

    val submitGoodsDataLiveData = MutableLiveData<HttpResponse<SubmitGoodsResponse>>()

    val submitNatureDataLiveData = MutableLiveData<HttpResponse<SubmitNatureResponse>>()

    val submitOrderIdDataLiveData = MutableLiveData<HttpResponse<SubmitOrderIdResponse>>()

    val orderListDataLiveData = SafeMutableLiveData<HttpResponse<OrderListResponse>>()

    val orderDetailsDataLiveData = MutableLiveData<HttpResponse<OrderDetailsResponse>>()

    val baseDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    val payDataLiveData = MutableLiveData<HttpResponse<PayResponse>>()

    val wxPayDataLiveData = MutableLiveData<HttpResponse<WXPayResponse>>()

    val aliPayDataLiveData = MutableLiveData<HttpResponse<AliPayResponse>>()

    /**
     * 获取默认地址
     * */
    fun addressDefault() {
        val listener =
            object :
                ViewModelSubscribeListener<AddressDefaultResponse>(addressDefaultDataLiveData) {}
        val memberItemDisposable: Disposable = orderManager.addressDefault(
            BaseApplication.userToken(), listener
        )
        dispose.add(memberItemDisposable)
    }

    /**
     *订单待提交页
     * */
    fun confirmOrder(entity: SubmitConfirmEntity) {
        val listener = object :
            ViewModelSubscribeListener<SubmitGoodsResponse>(submitGoodsDataLiveData) {}
        val confirmOrderDisposable: Disposable = orderManager.confirmOrder(
            BaseApplication.userToken(), entity, listener
        )
        dispose.add(confirmOrderDisposable)
    }

    /**
     *抵扣积分
     * */
    fun useScore() {
        val listener = object :
            ViewModelSubscribeListener<SubmitNatureResponse>(submitNatureDataLiveData) {}
        val confirmOrderDisposable: Disposable = orderManager.useScore(
            BaseApplication.userToken(), listener
        )
        dispose.add(confirmOrderDisposable)
    }

    /**
     *订单提交
     * */
    fun submitOrder(entity: SubmitConfirmEntity) {
        val listener = object :
            ViewModelSubscribeListener<SubmitOrderIdResponse>(submitOrderIdDataLiveData) {}
        val confirmOrderDisposable: Disposable = orderManager.submitOrder(
            BaseApplication.userToken(),  entity, listener
        )
        dispose.add(confirmOrderDisposable)
    }


    /**
     *订单支付
     * */
    fun orderPay(orderId: Int) {
        val listener = object :
            ViewModelSubscribeListener<PayResponse>(payDataLiveData) {
            override fun onSuccess(resp: PayResponse) {
                super.onSuccess(resp)
                EventBus.getDefault().post(
                    MainBean(type = OrderAction.SUCCESS, data = null)
                )
            }
        }
        val orderPayDisposable: Disposable = orderManager.orderPay(
            BaseApplication.userToken(), orderId.toString(), listener
        )
        dispose.add(orderPayDisposable)
    }


    var page: Int = 1
    var limit: Int = 20

    /**
     *获取订单列表
     * */
    fun orderList(type: Int, restart: Boolean = false) {
        val listener = object :
            ViewModelSubscribeListener<OrderListResponse>(orderListDataLiveData) {
            override fun onSuccess(resp: OrderListResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val orderListDisposable: Disposable = orderManager.orderList(
            BaseApplication.userToken(),
            page.toString(), limit.toString(), type.toString(), listener
        )
        dispose.add(orderListDisposable)
    }

    /**
     * 订单详情
     * */
    fun orderDetail(orderId: Int) {
        val listener = object :
            ViewModelSubscribeListener<OrderDetailsResponse>(orderDetailsDataLiveData) {}
        val orderPayDisposable: Disposable = orderManager.orderDetail(
            BaseApplication.userToken(), orderId.toString(), listener
        )
        dispose.add(orderPayDisposable)
    }

    /**
     *订单取消
     * */
    fun orderCancel(orderId: Int) {
        val listener = object :
            ViewModelSubscribeListener<BaseResponse>(baseDataLiveData) {
            override fun onSuccess(resp: BaseResponse) {
                super.onSuccess(resp)
                EventBus.getDefault().post(
                    MainBean(type = OrderAction.SUCCESS, data = null)
                )
            }
        }
        val orderCancelDisposable: Disposable = orderManager.orderCancel(
            BaseApplication.userToken(), orderId.toString(), listener
        )
        dispose.add(orderCancelDisposable)
    }

    /**
     *订单确认收货
     * */
    fun orderConfirm(orderId: Int) {
        val listener = object :
            ViewModelSubscribeListener<BaseResponse>(baseDataLiveData) {
            override fun onSuccess(resp: BaseResponse) {
                super.onSuccess(resp)
                EventBus.getDefault().post(
                    MainBean(type = OrderAction.SUCCESS, data = null)
                )
            }
        }
        val orderCancelDisposable: Disposable = orderManager.orderConfirm(
            BaseApplication.userToken(), orderId.toString(), listener
        )
        dispose.add(orderCancelDisposable)
    }


    /**
     *微信支付
     * */
    fun wxPay(orderId: String) {
        val listener = object :
            ViewModelSubscribeListener<WXPayResponse>(wxPayDataLiveData) {}
        val wxPayDisposable: Disposable = orderManager.wxPay(
            BaseApplication.userToken(), orderId, listener
        )
        dispose.add(wxPayDisposable)
    }

    /**
     *阿里支付
     * */
    fun aliPay(orderId: String) {
        val listener = object :
            ViewModelSubscribeListener<AliPayResponse>(aliPayDataLiveData) {}
        val aliPayDisposable: Disposable = orderManager.aliPay(
            BaseApplication.userToken(), orderId, listener
        )
        dispose.add(aliPayDisposable)
    }
}